package org.com.wareon.ftp;

import java.io.IOException;
import java.net.SocketException;
import java.util.ArrayList;

import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;



public class FTPObject {
	
	private ArrayList<ListenerFtp> listeners;
	private String hostName;
	private String username;
	private String password;
	private String ftpRootPath;
	private String contentSpliter;
	private FTPClient ftp;
	
	public FTPObject(String host,String user,String pwd){
		listeners = new ArrayList<ListenerFtp>();
		this.hostName=host;
		this.username=user;
		this.password=pwd;
		this.ftpRootPath="/";
		this.contentSpliter="/-/";
	}
	public void addListener(ListenerFtp l)
	{
		listeners.add(l);
	}
	public void removeListener(ListenerFtp l)
	{
		listeners.remove(l);
	}
	public void notifyListeners()
	{
		for(int i = 0; i < listeners.size(); i++)
		{
			ListenerFtp l = (ListenerFtp) listeners.get(i);
			l.updateFtp();
		}
	}
	
	public boolean connect() throws SocketException, IOException{
		this.ftp = new FTPClient();
		this.ftp.connect(hostName);
		this.ftp.login(username, password);
		int reply = ftp.getReplyCode();
		return this.ftp.isConnected();
	}
	public void disconnect() throws SocketException, IOException{
		
        if (ftp.isConnected())
        {
        	ftp.logout();
            ftp.disconnect();
        }
    
	}
	public String readFTPFolder(String dir) throws IOException {
		if(ftp.isConnected())
		{
	    	System.out.println(this.ftpRootPath+dir);
	    	if(ftp.changeWorkingDirectory(this.ftpRootPath+dir))
	    	{
	    		String folders="";
		        String folderFiles="";
		        String fileSize="";
		        FTPFile[] ftpFiles = ftp.listFiles();
		        for (FTPFile ftpFile : ftpFiles) {
		            if (ftpFile.getType() == FTPFile.FILE_TYPE){
		            	folderFiles+=":"+ftpFile.getName()+";";
		            	fileSize+=":"+String.valueOf(ftpFile.getSize())+";";
		            }else
		            {
		            	folders+=":"+ftpFile.getName()+";";
		            }
		        }
		        return folders+contentSpliter+folderFiles+contentSpliter+fileSize;
	    	}
	    	return "error"+contentSpliter+"Directory is not Exist.";
		}
		return "error"+contentSpliter+"FTP is not Connected to Server.";
    }
	public String readFTPFolderFileSize(String dir) throws IOException {
		if(ftp.isConnected())
		{
	    	System.out.println(this.ftpRootPath+dir);
	    	if(ftp.changeWorkingDirectory(this.ftpRootPath+dir))
	    	{
	    		String folders="";
		        String folderFiles="";
		        
		        FTPFile[] ftpFiles = ftp.listFiles();
		        for (FTPFile ftpFile : ftpFiles) {
		            if (ftpFile.getType() == FTPFile.FILE_TYPE){
		            	folderFiles+=":"+ftpFile.getName()+";";
		            	
		            }else
		            {
		            	folders+=":"+ftpFile.getName()+";";
		            }
		        }
		        return folders+"/-/"+folderFiles;
	    	}
	    	return "error/-/Directory is not Exist.";
		}
		return "error/-/FTP is not Connected to Server.";
    }
}

